﻿using ASP;
using Ext.Net;
using NOAH.CMS.Repository;

using System;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;

public partial class admin_Modules_NewsEvent_Images_AddOrEditImage : System.Web.UI.Page
{
    private string id = "";

    public void AddOrUpdate(object sender, DirectEventArgs e)
    {

        string mode = e.ExtraParams["mode"];

        try
        {
            int temp = 0;
            NOAH.CMS.Repository.Image newImage = null;
            ImageRepository repository = new ImageRepository();
            switch (mode)
            {
                case "add":
                    newImage = new NOAH.CMS.Repository.Image
                    {
                        CreatedOn = new DateTime?(DateTime.Now),
                        CreatedBy = BasePage.GetCurrentUserName()
                    };
                    break;

                case "edit":
                    newImage = repository.Get(int.Parse(this.id));
                    break;
            }
            if (this.txtImageDetailCategory.Value != null)
            {
                int.TryParse(this.txtImageDetailCategory.Value.ToString(), out temp);
                if (temp > 0)
                {
                    newImage.CategoryId = new int?(temp);
                }
            }
            newImage.EditedBy = BasePage.GetCurrentUserName();
            newImage.EditedOn = new DateTime?(DateTime.Now);
            newImage.Name = this.txtImageDetailTitle.Text;
            newImage.Description = this.txtImageDetailDescription.Text;
            if (this.txtImageDetailOrder.Value != null)
            {
                int.TryParse(this.txtImageDetailOrder.Value.ToString(), out temp);
                newImage.Order = new short?((short)temp);
            }
            newImage.Url = this.triggerAttachImage.Text;
            if (mode == "add")
            {
                repository.Add(newImage);
            }
            if (mode == "delete" )
            {
                repository.Delete(int.Parse(this.id));
             
            }

           
            repository.SubmitChanges();
            ExtNet.Msg.Hide();
            BasePage.RegisterCallBackFromChild(this.SM, string.Empty);
            Util.ShowMessage(Constants.UPDATE_SUCCESS);


            if (mode == "edit" || mode == "delete")
            {
                BasePage.RegisterCloseWindow(this.SM);
            }
        }
        catch (Exception ex)
        {
            ExtNet.Msg.Hide();
            Util.ShowMessage2(Constants.UPDATE_FAILURE + ex.Message + ex.StackTrace);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        id = base.Request.QueryString["id"] ?? "0";
        //Page.Response.Write(id);
        //return;

        if (!ExtNet.IsAjaxRequest)
        {
            if (this.id == "0")
            {
                this.btnUpdateImage.Visible = false;
                this.btnDeleteImage.Visible = false;
            }
            else
            {
                this.btnAdd.Visible = false;
                this.btnDeleteImage.Visible = false;
            }

            Store store = this.txtImageDetailCategory.GetStore();
            store.DataSource = new ImageCategoryRepository().GetAll();
            store.DataBind();
            this.ShowDetail();
        }
    }

    private void ResetAddOrEditImageForm()
    {
        this.txtImageDetailTitle.Text = "";
        this.txtImageDetailDescription.Text = "";
        this.triggerAttachImage.Text = "";
    }

    private void ResetUpdate()
    {
        this.txtImageDetailCategory.Reset();
    }

    private void ShowDetail()
    {
        if (this.id != "0")
        {
            using (ImageRepository repository = new ImageRepository())
            {
                NOAH.CMS.Repository.Image detailImage = repository.Get(int.Parse(this.id));
                this.txtImageDetailDescription.Text = detailImage.Description;
                this.txtImageDetailTitle.Text = detailImage.Name;
                this.txtImageDetailOrder.Text = detailImage.Order.ToString();
                this.triggerAttachImage.Text = detailImage.Url;
                int? temp = detailImage.CategoryId;
                int categoryId = temp.HasValue ? temp.GetValueOrDefault() : 0;
                string script = string.Format("setCombo({0})", categoryId);
                this.SM.Listeners.DocumentReady.Handler = script;
            }
        }
    }
}